package dk.contix.eclipse.hudson.views;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.IStructuredSelection;

import dk.contix.eclipse.hudson.Activator;
import dk.contix.eclipse.hudson.Job;

public final class SecurityTokenAction extends Action {
	private final HudsonView hudsonView;

	public SecurityTokenAction(HudsonView hudsonView) {
		this.hudsonView = hudsonView;
	}

	@Override
	public void run() {
		IStructuredSelection sel = (IStructuredSelection) hudsonView.viewer.getSelection();
		Job j = (Job) sel.getFirstElement();

		showSecurityTokenDialog(j);
	}
	
	private void showSecurityTokenDialog(Job j) {
		Preferences prefs = Activator.getDefault().getPluginPreferences();
		String sc = prefs.getString(Activator.PREF_SECURITY_TOKEN + "_" + j.getName());
		InputDialog dialog = new InputDialog(hudsonView.getSite().getShell(), "Enter security token for " + j.getName(), "Enter the security token for job " + j.getName(), sc, null);
		if (dialog.open() == InputDialog.OK) {
			prefs.setValue(Activator.PREF_SECURITY_TOKEN + "_" + j.getName(), dialog.getValue());
		}
	}


}
